package com.xunyin.modules.mtjoin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.modules.mtjoin.domain.MtJoin;
import com.xunyin.modules.mtjoin.service.MtJoinService;
import com.xunyin.modules.mtjoin.service.mapper.MtJoinMapper;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import com.xunyin.utils.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 招商加盟业务实现类
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MtJoinServiceImpl extends BaseServiceImpl<MtJoinMapper, MtJoin> implements MtJoinService {

    @Autowired
    private MtJoinMapper mtJoinMapper;

    @Override
    public PaginationResponse<MtJoin> queryListByPagination(PaginationRequest paginationRequest) {
        Page<MtJoin> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        LambdaQueryWrapper<MtJoin> lambdaQueryWrapper = Wrappers.lambdaQuery();
        String tel = paginationRequest.getSearchParams().get("tel") == null ? "" : paginationRequest.getSearchParams().get("tel").toString();
        if (StringUtils.isNotBlank(tel)) {
            lambdaQueryWrapper.eq(MtJoin::getTel, tel);
        }
        lambdaQueryWrapper.orderByDesc(MtJoin::getId);
        List<MtJoin> mtJoinList = mtJoinMapper.selectList(lambdaQueryWrapper);
        PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        PageImpl pageImpl = new PageImpl(mtJoinList, pageRequest, pageHelper.getTotal());
        PaginationResponse<MtJoin> paginationResponse = new PaginationResponse(pageImpl, MtJoin.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(mtJoinList);
        return paginationResponse;
    }

}
